package cn.miwu52.homeworkerp.dao;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

import cn.miwu52.homeworkerp.Util.MySqlOpenHelper;
import cn.miwu52.homeworkerp.bean.Entrepot;

public class EntrepotDao {
    private SQLiteDatabase db;
    private MySqlOpenHelper mySqlOpenHelper;

    public EntrepotDao(Context context) {
        try {
            mySqlOpenHelper = new MySqlOpenHelper(context, "erp", null, context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        db = mySqlOpenHelper.getWritableDatabase();
    }

    /**
     * 新增仓库
     *
     * @param name 仓库名字
     * @return 返回真 创建成功  返回假 创建失败
     */
    public boolean insertEntrepot(String name) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("entrepotName", name);
        long entrepot = db.insert("Entrepot", null, contentValues);
        if (entrepot == 0) {
            return false;
        }
        return true;
    }

    /**
     * 判断仓库名是否存在
     *
     * @param name 新仓库名
     * @return 返回真 则仓库名可用 返回假 则仓库名不可用
     */
    public boolean isEntrepotName(String name) {
        Cursor user = db.query("Entrepot", null, "entrepotName='" + name + "'", null, null, null, null);
        if (user.getCount() > 0) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 根据仓库名获取仓库相关信息
     *
     * @param name 要查询的仓库名
     * @return 返回仓库对象
     */
    @SuppressLint("Range")
    public Entrepot findEntrepotByName(String name) {
        Cursor user = db.query("Entrepot", null, "entrepotName='" + name + "'", null, null, null, null);
        Entrepot entrepot = new Entrepot();
        while (user.moveToNext()) {
            entrepot.setEntrepotName(user.getString(user.getColumnIndex("entrepotName")));
            entrepot.setId(user.getInt(user.getColumnIndex("id")));
            entrepot.setProductSum(user.getInt(user.getColumnIndex("productSum")));
        }
        return entrepot;
    }

    /**
     * 获取仓库列表
     *
     * @return 返回仓库列表
     */
    @SuppressLint("Range")
    public List<Entrepot> findEntrepotList() {
        ArrayList<Entrepot> entrepots = new ArrayList<>();
        Cursor entrepot = db.query("Entrepot", null, null, null, null, null, null);
        while (entrepot.moveToNext()) {
            Entrepot entrepot1 = new Entrepot();
            entrepot1.setEntrepotName(entrepot.getString(entrepot.getColumnIndex("entrepotName")));
            entrepot1.setProductSum(entrepot.getInt(entrepot.getColumnIndex("productSum")));
            entrepot1.setId(entrepot.getInt(entrepot.getColumnIndex("id")));
            entrepots.add(entrepot1);
        }
        return entrepots;
    }

    /**
     * 根据ID修改仓库名
     *
     * @param name 新仓库名
     * @param id   仓库ID
     * @return 修改成功返回真，修改失败返回假
     */
    public boolean updateEntrepotById(String name, Integer id) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("entrepotName", name);
        int entrepot = db.update("Entrepot", contentValues, "id=?", new String[]{id + ""});
        if (entrepot >= 0) {
            return true;
        }
        return false;
    }

    /**
     * 根据ID删除仓库
     *
     * @param id 仓库ID
     * @return 成功返回真，失败返回假
     */
    public boolean deleteEntrepotById(Integer id) {
        int entrepot = db.delete("Entrepot", "id=?", new String[]{id + ""});
        if (entrepot >= 0) {
            return true;
        }
        return false;
    }

}
